package com.video.video.service.impl;

import com.video.common.utils.uuid.IdUtils;
import com.video.video.domain.DEchargeRecord;
import com.video.video.domain.DUser;
import com.video.video.domain.app.dto.SubgiftVo;
import com.video.video.domain.dto.DSubgiftRecordDto;
import com.video.video.domain.vo.DSubgiftRecordQuery;
import com.video.video.service.IDEchargeRecordService;
import com.video.video.service.IDUserService;

import java.math.BigDecimal;
import java.util.List;
import com.video.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.video.video.mapper.DSubgiftRecordMapper;
import com.video.video.domain.DSubgiftRecord;
import com.video.video.service.IDSubgiftRecordService;

/**
 * 合伙人转增记录Service业务层处理
 *
 * @author ruoyi
 * @date 2022-09-27
 */
@Service
public class DSubgiftRecordServiceImpl implements IDSubgiftRecordService
{
    @Autowired
    private DSubgiftRecordMapper dSubgiftRecordMapper;
    @Autowired
    private IDUserService idUserService;
    @Autowired
    private IDEchargeRecordService idEchargeRecordService;

    /**
     * 查询合伙人转增记录
     *
     * @param id 合伙人转增记录主键
     * @return 合伙人转增记录
     */
    @Override
    public DSubgiftRecord selectDSubgiftRecordById(String id)
    {
        return dSubgiftRecordMapper.selectDSubgiftRecordById(id);
    }

    /**
     * 查询合伙人转增记录列表
     *
     * @param query 合伙人转增记录
     * @return 合伙人转增记录
     */
    @Override
    public List<DSubgiftRecordDto> selectDSubgiftRecordList(DSubgiftRecordQuery query)
    {
        return dSubgiftRecordMapper.selectDSubgiftRecordList(query);
    }

  @Override
  public int insertAPPSubgiftRecord(SubgiftVo vo) {
    DUser hehuoren= idUserService.selectDUserById(vo.getHid());
    DUser huiyuan= idUserService.selectDUserById(vo.getUid());
    //插入转赠记录
    DSubgiftRecord dSubgiftRecord=new DSubgiftRecord();
    dSubgiftRecord.setId(IdUtils.getId());
    dSubgiftRecord.setHid(vo.getHid());
    dSubgiftRecord.setUid(vo.getUid());
    dSubgiftRecord.setAmount(vo.getAmount());
    dSubgiftRecord.setCurrentAmount(hehuoren.getBalance());

    dSubgiftRecord.setGiveAmount(vo.getGiveAmount());
    dSubgiftRecord.setCurrentGiveAmount(hehuoren.getGiveBalance());

    dSubgiftRecord.setCreateTime(DateUtils.getNowDate());
    dSubgiftRecordMapper.insertDSubgiftRecord(dSubgiftRecord);
    //插入充值记录
    DEchargeRecord dEchargeRecord=new DEchargeRecord();
    dEchargeRecord.setUid(dSubgiftRecord.getUid());//会员id
    dEchargeRecord.setHid(dSubgiftRecord.getHid());//合伙人id
    dEchargeRecord.setEchargeType("2");//充值方式：1 平台 2 合伙人
    dEchargeRecord.setType("2");//支付渠道：1 支付宝 2 微信 3银行卡 4 现金
    dEchargeRecord.setAccountNumber("1111111");//充值账号
    dEchargeRecord.setCurrentAmount(huiyuan.getBalance());//当前会员余额金额
    dEchargeRecord.setAmount(dSubgiftRecord.getAmount());//充值金额

    dEchargeRecord.setCurrentGiveAmount(huiyuan.getGiveBalance()); //当前会员赠送的余额
    dEchargeRecord.setGiveAmount(dSubgiftRecord.getGiveAmount()); //赠送金额

    idEchargeRecordService.insertDEchargeRecord(dEchargeRecord);

    //修改合伙人余额 和赠送金额
    BigDecimal giveDecimal =  dSubgiftRecord.getAmount().add(dSubgiftRecord.getGiveAmount());
    if(hehuoren.getBalance().compareTo(giveDecimal) < 0){
        //先从余额中扣减 再从积分中扣减
        hehuoren.setGiveBalance(hehuoren.getGiveBalance().subtract(giveDecimal.subtract(hehuoren.getBalance())));
        hehuoren.setBalance(new BigDecimal("0.00"));
    }else{
        //直接从 余额中扣减即可
        hehuoren.setBalance(hehuoren.getBalance().subtract(giveDecimal));
    }

    idUserService.updateDUserInfo(hehuoren);

    // 修改会员余额 和赠送余额
    huiyuan.setBalance(huiyuan.getBalance().add(dSubgiftRecord.getAmount()));
    huiyuan.setGiveBalance(huiyuan.getGiveBalance().add(dSubgiftRecord.getGiveAmount()));
    idUserService.updateDUserInfo(huiyuan);
    return 1;
  }

  /**
     * 新增合伙人转增记录
     *
     * @param dSubgiftRecord 合伙人转增记录
     * @return 结果
     */
    @Override
    public int insertDSubgiftRecord(DSubgiftRecord dSubgiftRecord)
    {

       DUser hehuoren= idUserService.selectDUserById(dSubgiftRecord.getHid());
       DUser huiyuan= idUserService.selectDUserById(dSubgiftRecord.getUid());
       //插入转赠记录
       dSubgiftRecord.setCurrentAmount(hehuoren.getBalance());
       dSubgiftRecord.setId(IdUtils.getId());
       dSubgiftRecord.setCreateTime(DateUtils.getNowDate());
       dSubgiftRecordMapper.insertDSubgiftRecord(dSubgiftRecord);
       //插入充值记录
        DEchargeRecord dEchargeRecord=new DEchargeRecord();
        dEchargeRecord.setUid(dSubgiftRecord.getUid());//会员id
        dEchargeRecord.setHid(dSubgiftRecord.getHid());//合伙人id
        dEchargeRecord.setEchargeType("2");//充值方式：1 平台 2 合伙人
        dEchargeRecord.setType("2");//支付渠道：1 支付宝 2 微信 3银行卡 4 现金
        dEchargeRecord.setAccountNumber("1111111");//充值账号
        dEchargeRecord.setCurrentAmount(huiyuan.getBalance());//当前会员余额金额
        dEchargeRecord.setAmount(dSubgiftRecord.getAmount());//充值金额
        idEchargeRecordService.insertDEchargeRecord(dEchargeRecord);
        //修改合伙人余额
        hehuoren.setBalance(hehuoren.getBalance().subtract(dSubgiftRecord.getAmount()));
        idUserService.updateDUserInfo(hehuoren);
       // 修改会员余额
        huiyuan.setBalance(huiyuan.getBalance().add(dSubgiftRecord.getAmount()));
        idUserService.updateDUserInfo(huiyuan);
        return 1;
    }

    /**
     * 修改合伙人转增记录
     *
     * @param dSubgiftRecord 合伙人转增记录
     * @return 结果
     */
    @Override
    public int updateDSubgiftRecord(DSubgiftRecord dSubgiftRecord)
    {
        dSubgiftRecord.setUpdateTime(DateUtils.getNowDate());
        return dSubgiftRecordMapper.updateDSubgiftRecord(dSubgiftRecord);
    }

    /**
     * 批量删除合伙人转增记录
     *
     * @param ids 需要删除的合伙人转增记录主键
     * @return 结果
     */
    @Override
    public int deleteDSubgiftRecordByIds(String[] ids)
    {
        return dSubgiftRecordMapper.deleteDSubgiftRecordByIds(ids);
    }

    /**
     * 删除合伙人转增记录信息
     *
     * @param id 合伙人转增记录主键
     * @return 结果
     */
    @Override
    public int deleteDSubgiftRecordById(String id)
    {
        return dSubgiftRecordMapper.deleteDSubgiftRecordById(id);
    }
}
